package graph1;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @program: data_structures_algorithms
 * @description: 无向图
 * @author: lld
 * @create: 2020-11-11 16:35
 **/
public class Graph {
    private ArrayList<String> vertexList;//存储顶点集合
    private int[][] edges;//存储图的邻接矩阵
    private int numOfEdges;//边的数目

    public Graph(int n){//初始化
        edges=new int[n][n];
        vertexList=new ArrayList<>(n);
        numOfEdges=0;
    }
    //图常用方法
    //返回节点的个数
    public int getNumOfVertex(){
        return vertexList.size();
    }
    //得到边的数目
    public int getNumOfEdges(){
        return numOfEdges;
    }
    //返回节点对应的数据0->A,1->B
    public String getValueByIndex(int i){
        return vertexList.get(i);
    }
    //显示矩阵
    public void showGraph(){
        for(int[] link:edges){
            System.err.println(Arrays.toString(link));
        }
    }
    //添加节点
    public void insertVertex(String vertex){
        vertexList.add(vertex);
    }

    /**
    * @Description: 添加边
    * @Param: [v1 节点的下标确定节点, v2, weight 是否直接连接1/0]
    * @return: void
    **/
    public void insertVertex(int v1,int v2,int weight){
        edges[v1][v2]=weight;
        edges[v2][v1]=weight;
        numOfEdges++;
    }

    public static void main(String[] args) {
        String[] vertexes={"A","B","C","D","E"};
        Graph graph=new Graph(5);
        //向图添加节点
        for(String vertex:vertexes){
            graph.insertVertex(vertex);
        }
        //添加边A-B,A-C,B-C,B-D,B-E
        graph.insertVertex(0, 1, 1);
        graph.insertVertex(0, 2, 1);
        graph.insertVertex(1, 2, 1);
        graph.insertVertex(1, 3, 1);
        graph.insertVertex(1, 4, 1);

        //显示
        graph.showGraph();
    }
}
